﻿using System;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    public class MessageController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 收件箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessagePaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 发件箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetOutboxPaged(DataTableParameter parameters)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageOutboxPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 草稿箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetDraftboxPaged(DataTableParameter parameters)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageDraftboxPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        /// <summary>
        /// 垃圾箱
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetDustbinPaged(DataTableParameter parameters)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetMessageDustbinPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        [AutomaticTemplate]
        public ActionResult Details(Guid id)
        {
            MessageDTO dto;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                dto = proxy.Channel.GetMessageInfo(id);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return View(dto.To<MessageModel>());
        }

        public ActionResult Create()
        {
            return View("Create", new MessageModel());
        }

        [HttpPost]
        public ActionResult Send(MessageModel message)
        {
            MessageDTO dto = message.To<MessageDTO>();

            using (var proxy = new ServiceProxy<IUserService>())
            {
                var res = proxy.Channel.SendMessage(dto);

                return this.JsonNet(res);
            }
        }
        public ActionResult Draft(MessageModel message)
        {
            MessageDTO dto = message.To<MessageDTO>();

            using (var proxy = new ServiceProxy<IUserService>())
            {
                var res = proxy.Channel.DraftMessage(dto);

                return this.JsonNet(res);
            }
        }
        [HttpPost]
        public ActionResult Delete(Guid[] ids)
        {
            Result res;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                res = proxy.Channel.DeleteMessages(ids);
            }

            return this.JsonNet(res);
        }
    }
}